/**
 * @(#) CommonProxy.as
 *
 * @date		2009. 04. 6.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	 국경일 및 휴일정보 Proxy - Proxy
 * 				모든 proxy는 여기에서 상속 받는다.
 */
package com.skt.common_hr.common.puremvc
{
	import com.skt.common_hr.common.Variables;
	import com.wcl.dataloader.*;
	
	import flash.net.URLLoader;
	import flash.utils.Dictionary;
	
	import mx.core.ClassFactory;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import org.puremvc.as3.multicore.interfaces.IProxy;
	import org.puremvc.as3.multicore.patterns.proxy.Proxy;
	
	public class CommonProxy extends Proxy implements IProxy
	{
		protected var loader:URLLoader = new URLLoader();
		
		public var retCode:int;
		public var retText:String;
		public var faultDetail:String;
		
		private var resultCallbackMap:Dictionary = new Dictionary();
		private var faultCallbackMap:Dictionary = new Dictionary();
		
		private var resultCallback:Function = null;
		private var faultCallback:Function = null;
		
		//silver
		private var rtnFunctionCallback:Function = null;
		private var rtnObj:Object = null;
		
		
		public function get faultMessage():String
		{
			return "[" + retCode + "] " + retText; 
		}
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function CommonProxy(name:String, data:Object=null)
		{
			super(name, data);
		}
		
		private function resetSafeTimer():void
		{
			Variables.safeTimer.reset();
			Variables.safeTimer.start();
		}
		
		protected function loadData(rfcName:String, param:Object, result:Function, fault:Function, 
									rtnFunction:Function=null, _rtnObj:Object = null):void
		{
			resetSafeTimer();
			CursorManager.setBusyCursor();
			var loader:IDataLoader = DataLoaderFactory.getDataLoader(rfcName, Variables.dataSource, DataLoaderFactory.TYPE_JSON_HTTP); 

			resultCallback = result;
			faultCallback = fault;
			rtnFunctionCallback = rtnFunction;
			rtnObj = _rtnObj;
			loader.load(param, parseResult, parseFault); 
		}


		public function exLoadData(rfcName:String, param:Object, result:Function, fault:Function):void{
			loadData(rfcName, param, result, fault);
		}
		
		public function exSaveData(rfcName:String, param:Object, table:Object, result:Function, fault:Function):void
		{
			saveData(rfcName, param, table, result, fault);
		}
		
		public function exLoadMultiData(rfcName:String, param:Object, result:Function, type:String=null):void{
			loadMultiData(rfcName, param, result, type);
		}
		
		public function exSaveMultiData(rfcName:String, param:Object, table:Object, result:Function, type:String=null):void{
			saveMultiData(rfcName, param, table, result, type);
		}
		
				
		protected function loadMultiData(rfcName:String, param:Object, result:Function, type:String=null):void
		{
			resetSafeTimer();
			CursorManager.setBusyCursor();
			var loader:IDataLoader = DataLoaderFactory.getDataLoader(rfcName, Variables.dataSource, DataLoaderFactory.TYPE_JSON_HTTP);
			var receiver:DataReceiver = new DataReceiver(this, result, type);			
			loader.load(param, receiver.resultHandler, receiver.faultHandler); 
		}
		
		
		
		protected function saveData(rfcName:String, param:Object, table:Object, result:Function, fault:Function, rtnFunction:Function=null):void
		{
			resetSafeTimer();
			CursorManager.setBusyCursor();
			var loader:IDataLoader = DataLoaderFactory.getDataLoader(rfcName, Variables.dataSource, DataLoaderFactory.TYPE_JSON_HTTP); 
			resultCallback = result;
			faultCallback = fault;
			rtnFunctionCallback = rtnFunction;
			
			loader.save(param, table, parseResult, parseFault); 
		}
		
		protected function saveMultiData(rfcName:String, param:Object, table:Object, result:Function, type:String=null):void
		{
			resetSafeTimer();
			CursorManager.setBusyCursor();
			var loader:IDataLoader = DataLoaderFactory.getDataLoader(rfcName, Variables.dataSource, DataLoaderFactory.TYPE_JSON_HTTP); 
			var receiver:DataReceiver = new DataReceiver(this, result);
			loader.save(param, table, receiver.resultHandler, receiver.faultHandler); 
		}
		
		
		
		private function parseResult(event:ResultEvent):void
		{
			retCode = event.result["RETCODE"];
			retText = event.result["RETTEXT"];
			
			if(rtnFunctionCallback!=null) {
				event.result.func = rtnFunctionCallback;				
			}
			if(rtnObj!=null) {
				event.result.rtnObj = rtnObj;
			}
			
			
			resultCallback(event);
			CursorManager.removeBusyCursor();
		}
		
		private function parseFault(event:FaultEvent):void
		{
			retCode = -1;
			retText = "["+event.fault.faultCode+"]"+event.fault.faultString;
			faultDetail = event.fault.faultDetail;
			faultCallback(event);	
			CursorManager.removeBusyCursor();
		}
		
		protected function copyObject(type:Class, source:Object):Object {
			var factory:ClassFactory = new ClassFactory(type);
			var dest:* = new type();
			for (var prop:* in dest) {
				//trace(prop);
				dest[prop] = source[prop]; 
			}
			return dest;
		}
	}
}